using System;
using Habanero.Base;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.BO
{
    /// <summary>
    /// A <see cref="IBusinessObjectRule"/> that validates that the single side of a Many-To-One <see cref="DMRelationship"/>
    /// has the Single side <see cref="DeleteParentAction"/> set to <see cref="DeleteParentAction.DoNothing"/>.
    /// </summary>
    public class DMRelationshipOneToOneOneSideMustBeAssociation : IBusinessObjectRule
    {
        private IDMRelationship _dmRelationship;

//        public DMRelationshipOneToOneOneSideMustBeAssociation(IDMRelationship dmRelationship)
//        {
//            if (dmRelationship == null) throw new ArgumentNullException("dmRelationship");
//            _dmRelationship = dmRelationship;
//        }

        #region Implementation of IBusinessObjectRule

        /// <summary>
        /// Indicates whether the property value is valid against the rules
        /// </summary>
        /// <returns>
        /// Returns true if valid
        /// </returns>
        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _dmRelationship = (IDMRelationship) bo;
            if (_dmRelationship.ReverseRelationship == null) return true;
            return !_dmRelationship.IsOneToOne || IsOneDirectionAssociation();
        }

        private bool IsOneDirectionAssociation()
        {
            return !_dmRelationship.IsCompositionOrAggregation || !_dmRelationship.ReverseRelationship.IsCompositionOrAggregation;
        }

        /// <summary>
        /// Returns the rule name
        /// </summary>
        public string Name
        {
            get { return "DMRelationshipOneToOneOneSideMustBeAssociation"; }
        }

        /// <summary>
        /// Returns the error message for if the rule fails.
        /// </summary>
        public string Message
        {
            get
            {
                string reverseRelMessage = "";
                var reverseRelationship = _dmRelationship.ReverseRelationship;
                if (reverseRelationship != null)
                {
                    reverseRelMessage = " or the reverse relationship '"
                                        + reverseRelationship + "' on Class '" + reverseRelationship.ClassDM + "'" ;
                }
                return "For a One To One Relationship either the Relationship '" + _dmRelationship
                       + "' on Class '" + _dmRelationship.ClassDM + "'" + reverseRelMessage + " must be 'Association'";
            }
        }

        /// <summary>
        /// The <see cref="P:Habanero.Base.IBusinessObjectRule.ErrorLevel" /> for this BusinessObjectRule e.g. Warning, Error. 
        /// </summary>
        public ErrorLevel ErrorLevel
        {
            get { return Habanero.Base.ErrorLevel.Error; }
        }

        #endregion
    }
}